<?php
namespace App\Http\Controllers\Admin;
use App\Repositories\Contracts\ManagerInterface;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ManagerRequest;
use Flash;
class ManagerController extends Controller
{
    protected $manager;

    /**
     * ManagerController constructor.
     * @param ManagerInterface $manager
     */
    public function __construct(ManagerInterface $manager)
    {
        $this->manager = $manager;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(){
        $data = $this->manager->allUser(config('admin.gloables.perPage'));
        return view('admin.manager.index',compact('data'));
    }

    /**
     * 搜索查询器
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function search(Request $request){
        $data = $this->manager->searchKeyWords($request->except('_token'),config('admin.gloables.perPage'));
        return view('admin.manager.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.manager.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ManagerRequest $request)
    {
        $this->manager->create($request->except('_token'));
//        if($data){
////            return back()->with('errors','管理员添加成功');
//            Flash::success('管理员添加成功!');
//        }else{
//            Flash::error('管理员添加失败!');
//        }
        return redirect('admin/manager');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id){
        $data = $this->manager->findById($id);
        return view('admin.manager.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id){
        $this->manager->update($request->except('_method','_token'),$id);
        return redirect('admin/manager');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id){
        $data = $this->manager->delete($id);
        return $data;
    }
}
